package cn.liwq.demo.disruptor;

import com.lmax.disruptor.ExceptionHandler;
import com.lmax.disruptor.dsl.Disruptor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lweiqiang
 * @date 2023/5/5
 */
@Slf4j
public class TaskExceptionHandler implements ExceptionHandler<TaskEvent> {

    private Disruptor<TaskEvent> disruptor;

    public TaskExceptionHandler(Disruptor<TaskEvent> disruptor) {
        this.disruptor = disruptor;
    }

    @Override
    public void handleEventException(Throwable ex, long sequence, TaskEvent event) {
        log.error("Exception processing: {} {}", sequence, event, ex);
        disruptor.halt();
        throw new RuntimeException(ex);
    }

    @Override
    public void handleOnStartException(final Throwable ex) {
        log.error("Exception during onStart()", ex);
    }

    @Override
    public void handleOnShutdownException(final Throwable ex) {
        log.error("Exception during onShutdown()", ex);
    }
}
